layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    /**
     * 订单记录管理
     */
    var FlowOrder = {
        tableId: "flowOrderTable"
    };

    /**
     * 初始化表格的列
     */
    FlowOrder.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'orderId', hide: true, title: 'ID'},
            {field: 'orderCode', sort: true, title: '订单编号'},
            {field: 'serviceName', sort: true, title: '服务类型'},
            {field: 'orderStatus', sort: true, title: '订单状态',templet:function (row) {
                if(row.orderStatus == 0){
                    return "待客服确认";
                }else if(row.orderStatus == 1){
                    return "客服处理中";
                }else if(row.orderStatus == 2){
                    return "等待付款";
                }else if(row.orderStatus == 3){
                    return "等待搬运服务";
                }else if(row.orderStatus == 4){
                    return "服务完成";
                }else if(row.orderStatus == 5){
                    return "订单完成";
                }else if(row.orderStatus == 6){
                    return "关闭订单";
                }else {
                    return "";
                }
            }},
            {field: 'storeName', sort: true, title: '服务网点'},
            {field: 'name', sort: true, title: '联系姓名'},
            {field: 'phone', sort: true, title: '联系电话'},
            {field: 'memberAccount', sort: true, title: '绑定手机'},
            {field: 'account', sort: true, title: '处理人'},
            {field: 'serviceCity', sort: true, title: '服务城市'},
            {field: 'orderFrom', sort: true, title: '订单来源'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'superUserId', sort: true, title: '佣金订单'},
            {align: 'center', toolbar: '#tableBar', title: '操作',width:350}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FlowOrder.search = function () {
        var queryData = {};

        queryData['orderCode'] = $('#orderCode').val();
        queryData['name'] = $('#name').val();
        queryData['phone'] = $('#phone').val();
        queryData['orderFrom'] = $('#orderFrom').val();
        queryData['orderType'] = $('#orderType').val();

        table.reload(FlowOrder.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      FlowOrder.openEditDlg = function (data) {
          func.open({
              title: '修改订单记录',
              content: Feng.ctxPath + '/flowOrder/edit?orderId=' + data.orderId,
              tableId: FlowOrder.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    FlowOrder.exportExcel = function () {
        var checkRows = table.checkStatus(FlowOrder.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FlowOrder.tableId,
        url: Feng.ctxPath + '/flowOrder/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: FlowOrder.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        FlowOrder.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        FlowOrder.exportExcel();
    });

    // 搜索按钮点击事件
    $('#btnReset').click(function () {
        $("#orderCode").val("");
        $("#name").val("");
        $("#phone").val("");
        $("#from").val("");
        $('#orderFrom').val("");
        $("#orderType").val("");
        form.render('select');
    });

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FlowOrder.onConfirmOrderItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/flowOrder/confirmOrder", function (data) {
                if(data.code == 200){
                    Feng.success("操作成功!");
                    table.reload(FlowOrder.tableId);
                }else {
                    Feng.error(data.message);
                }

            }, function (data) {
                Feng.error("操作失败!" + data.responseJSON.message + "!");
            });
            ajax.set("orderId", data.orderId);
            ajax.start();
        };
        Feng.confirm("是否设为处理中?", operation);
    };

    //退款审核
    FlowOrder.refundOrderItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/flowOrder/canselOrder", function (data) {
                if(data.code == 200){
                    Feng.success("操作成功!");
                    table.reload(FlowOrder.tableId);
                }else {
                    Feng.error(data.message);
                }

            }, function (data) {
                Feng.error("操作失败!" + data.responseJSON.message + "!");
            });
            ajax.set("orderId", data.orderId);
            ajax.start();
        };
        Feng.confirm("用户退款审核通过?", operation);
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FlowOrder.onCanPayOrderItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/flowOrder/canPayOrder", function (data) {
                if(data.code == 200){
                    Feng.success("操作成功!");
                    table.reload(FlowOrder.tableId);
                }else {
                    Feng.error(data.message);
                }

            }, function (data) {
                Feng.error("操作失败!" + data.responseJSON.message + "!");
            });
            ajax.set("orderId", data.orderId);
            ajax.start();
        };
        Feng.confirm("是否设为可付款?", operation);
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FlowOrder.onPayOrderItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/flowOrder/payOrder", function (data) {
                if(data.code == 200){
                    Feng.success("操作成功!");
                    table.reload(FlowOrder.tableId);
                }else {
                    Feng.error(data.message);
                }

            }, function (data) {
                Feng.error("操作失败!" + data.responseJSON.message + "!");
            });
            ajax.set("orderId", data.orderId);
            ajax.start();
        };
        Feng.confirm("订单确认已付款?", operation);
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FlowOrder.onCanselOrderItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/flowOrder/canselOrder", function (data) {
                if(data.code == 200){
                    Feng.success("操作成功!");
                    table.reload(FlowOrder.tableId);
                }else {
                    Feng.error(data.message);
                }

            }, function (data) {
                Feng.error("操作失败!" + data.responseJSON.message + "!");
            });
            ajax.set("orderId", data.orderId);
            ajax.start();
        };
        Feng.confirm("是否取消订单?", operation);
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FlowOrder.onServiceSuccessItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/flowOrder/serviceSuccess", function (data) {
                if(data.code == 200){
                    Feng.success("操作成功!");
                    table.reload(FlowOrder.tableId);
                }else {
                    Feng.error(data.message);
                }

            }, function (data) {
                Feng.error("操作失败!" + data.responseJSON.message + "!");
            });
            ajax.set("orderId", data.orderId);
            ajax.start();
        };
        Feng.confirm("订单服务完成?", operation);
    };

    // 工具条点击事件
    table.on('tool(' + FlowOrder.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            FlowOrder.openEditDlg(data);
        } else if(layEvent === 'confirmOrder'){
            FlowOrder.onConfirmOrderItem(data);
        } else if(layEvent === 'canPayOrder'){
            FlowOrder.onCanPayOrderItem(data);
        } else if(layEvent === 'payOrder'){
            FlowOrder.onPayOrderItem(data);
        } else if(layEvent === 'serviceSuccess'){
            FlowOrder.onServiceSuccessItem(data);
        } else if(layEvent === 'canselOrder'){
            FlowOrder.onCanselOrderItem(data);
        }else if(layEvent === 'refundOrder'){
            FlowOrder.refundOrderItem(data);
        }
    });

    //类型
    var serviceTypeAjax = new $ax(Feng.ctxPath + "/flowOrder/serviceType");
    var serviceTypeResult = serviceTypeAjax.start();
    if(serviceTypeResult.code == 200){
        var serviceTypeStr = "<option value=''>请选择服务类型</option>";
        for (var i = 0;i<serviceTypeResult.data.length;i++){
            serviceTypeStr += "<option value='"+serviceTypeResult.data[i].serviceId+"'>"+serviceTypeResult.data[i].serviceName+"</option>";
        }
        $("#orderType").html(serviceTypeStr);
        form.render('select');
    }
});
